package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TsExamSyllabusVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbSemesterMapper;
import com.ruoyi.basice_info.domain.TbSemester;
import com.ruoyi.basice_info.service.ITbSemesterService;

/**
 * 学期设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-07
 */
@Service
public class TbSemesterServiceImpl implements ITbSemesterService
{
    @Autowired
    private TbSemesterMapper tbSemesterMapper;

    @Autowired
     private TsExamSyllabusVoMapper tsExamSyllabusVoMapper;
    /**
     * 查询学期设置
     *
     * @param semesterId 学期设置主键
     * @return 学期设置
     */
    @Override
    public TbSemester selectTbSemesterBySemesterId(Long semesterId)
    {
        return tbSemesterMapper.selectTbSemesterBySemesterId(semesterId);
    }

    @Override
    public TbSemester getTbSemesterBySemesterByCode(String code) {
        return tbSemesterMapper.getTbSemesterBySemesterByCode(code);
    }

    @Override
    public TbSemester getTbSemesterBySemesterByName(String name) {
        return tbSemesterMapper.getTbSemesterBySemesterByName(name);
    }

    /**
     * 查询学期设置列表
     *
     * @param tbSemester 学期设置
     * @return 学期设置
     */
    @Override
    public List<TbSemester> selectTbSemesterList(TbSemester tbSemester)
    {
        return tbSemesterMapper.selectTbSemesterList(tbSemester);
    }

    /**
     * 新增学期设置
     *
     * @param tbSemester 学期设置
     * @return 结果
     */
    @Override
    public int insertTbSemester(TbSemester tbSemester)
    {
        return tbSemesterMapper.insertTbSemester(tbSemester);
    }

    /**
     * 修改学期设置
     *
     * @param tbSemester 学期设置
     * @return 结果
     */
    @Override
    public int updateTbSemester(TbSemester tbSemester)
    {
        return tbSemesterMapper.updateTbSemester(tbSemester);
    }

    /**
     * 批量删除学期设置
     *
     * @param semesterIds 需要删除的学期设置主键
     * @return 结果
     */
    @Override
    public int deleteTbSemesterBySemesterIds(Long[] semesterIds)
    {
        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(semesterIds);
        for (Long item: longList) {
            TsExamSyllabusVo syllabus = new TsExamSyllabusVo();
            syllabus.setSemesterId(item);
            // 去子表中查询一下
            List<TsExamSyllabusVo> syllabusList = tsExamSyllabusVoMapper.selectTsExamSyllabusList(syllabus);
            // 判断一下是否有有元素
            if(syllabusList.size()<=0) {
                // 在子表中不存在则删除
                num = tbSemesterMapper.deleteTbSemesterBySemesterId(item);
            } else {
                nums ++;
            }
        }
        if(longList.size() == 1) {
            if(nums>0) {
                throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
            } else  {
                if (nums > 0) {
                    throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
                }
            }
        }
//        return tbSemesterMapper.deleteTbSemesterBySemesterIds(semesterIds);
        return num;
    }

    /**
     * 删除学期设置信息
     *
     * @param semesterId 学期设置主键
     * @return 结果
     */
    @Override
    public int deleteTbSemesterBySemesterId(Long semesterId)
    {
        return tbSemesterMapper.deleteTbSemesterBySemesterId(semesterId);
    }
}
